package com.zxj.friend.controller.user;


import com.zxj.common.core.contains.HttpConstants;
import com.zxj.common.core.controller.BaseController;
import com.zxj.common.core.domain.R;
import com.zxj.common.core.domain.vo.PageVO;
import com.zxj.friend.aspect.CheckUserStatus;
import com.zxj.friend.domain.user.dto.UserCodeDTO;
import com.zxj.friend.domain.user.dto.UserLoginDTO;
import com.zxj.friend.domain.user.dto.UserUpdateDTO;
import com.zxj.friend.domain.user.vo.LoginUserVO;
import com.zxj.friend.domain.user.vo.UserLoginVO;
import com.zxj.friend.domain.user.vo.UserVO;
import com.zxj.friend.service.user.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author zxj2022
 * @since 2025-01-01
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "用户管理相关接口", description = "用户管理相关接口")
public class UserController extends BaseController {
    private final IUserService userService;

    //  /user/sendCode
    @PostMapping("sendCode")
    @Operation(summary = "发送验证码")
    public R<Void> sendCode(@RequestBody UserCodeDTO userCodeDTO) {
        log.info("接收到发送验证码请求, 请求参数：{}", userCodeDTO);
        return toR(userService.sendCode(userCodeDTO)) ;
    }


    // /code/login
    //  post
    @PostMapping("/code/login")
    @Operation(summary = "验证码登录")
    public R<UserLoginVO> codeLogin(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("接收到验证码登录请求, 请求参数：{}", userLoginDTO);
        return R.ok(userService.codeLogin(userLoginDTO));
    }

    @DeleteMapping("/logout")
    @Operation(summary = "退出登录")
    @Parameter(name = HttpConstants.AUTHENTICATION, description = "jwt token", required = true,  in = ParameterIn.HEADER)
    public R<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        log.info("接收到退出登录请求, 请求参数：{}", token);
        return toR(userService.logout(token));
    }

    @GetMapping("/info")
    @Operation(summary = "从token中获取用户登录信息")
    @Parameter(name = HttpConstants.AUTHENTICATION, description = "jwt token", required = true,  in = ParameterIn.HEADER)
    public R<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        log.info("接收到获取用户信息请求, 请求参数：{}", token);
        return R.ok(userService.info(token));
    }

    @GetMapping("/detail")
    @Operation(summary = "从token中获取用户详细信息")
    @Parameter(name = HttpConstants.AUTHENTICATION, description = "jwt token", required = true,  in = ParameterIn.HEADER)
    public R<UserVO> detail() {
        log.info("接收到获取用户详情请求");
        return R.ok(userService.detail());
    }

    @PutMapping("/edit")
    @Operation(summary = "编辑修改用户详细信息")
    @Parameter(name = HttpConstants.AUTHENTICATION, description = "jwt token", required = true,  in = ParameterIn.HEADER)
    @CheckUserStatus
    public R<Void> edit(@RequestBody UserUpdateDTO userUpdateDTO) {
        log.info("接收到编辑用户信息请求, 请求参数：{}", userUpdateDTO);
        return toR(userService.edit(userUpdateDTO));
    }

    @PutMapping("/head-image/update")
    @Operation(summary = "更新用户头像")
    @Parameter(name = HttpConstants.AUTHENTICATION, description = "jwt token", required = true,  in = ParameterIn.HEADER)
    @CheckUserStatus
    public R<Void> updateHeadImage(@RequestBody UserUpdateDTO userUpdateDTO) {
        log.info("接收到更新用户头像请求, 请求参数：{}", userUpdateDTO.getHeadImage());
        return toR(userService.updateHeadImage(userUpdateDTO.getHeadImage()));
    }
}
